<?php

    namespace Product\Controller;

    use Common\Controller\AdminbaseController;

    class AdminProductController extends AdminbaseController {

        protected $m_products;
        protected $m_product_type;

        function _initialize() {
            parent::_initialize();
            $this->m_products= D("Product/Products");
            $this->m_product_type= D("Product/ProductType");
        }

        /**
         * 商品分类管理
         */
        public function index(){
            $s_tid = I("post.s_tid");
            $keyword = I("post.keyword");
            $this->assign("s_tid",$s_tid);
            $this->assign("keyword",$keyword);
            $where = array();
            if(!empty($s_tid)){
                $where['tid'] = $s_tid;
            }
            if(!empty($keyword)){
                $where['pname'] = array('like',"%$keyword%");
            }
            $type_list = $this->m_product_type->select();
            $this->assign("type_list",$type_list);
            $count=$this->m_products->count();
            $page = $this->page($count, 15);
            $list = $this->m_products->field("p.*,t.title")->alias("p")
                ->where($where)
                ->join("lz_product_type t ON t.id = p.tid")
                ->order("create_time desc")
                ->limit($page->firstRow , $page->listRows)
                ->select();
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 排序
         */
        public function listorders(){
            $status = parent::_listorders($this->m_products,'sort');
            if ($status) {
                $this->success("排序更新成功！");
            } else {
                $this->error("排序更新失败！");
            }
        }

        // 添加商品
        public function add(){
            $type_list = $this->m_product_type->select();
            $this->assign("type_list",$type_list);
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item=$this->m_products->where(array('id'=>$id))->find();
                $item['pictures'] = explode(',',$item['pictures']);
                $this->assign("item",$item);
            }

            $this->display();
        }

        // 文章添加提交
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id");
                $insert = array(
                    'tid'=>I("post.tid"),
                    'pname'=>I("post.pname"),
                    'is_hot'=>I("post.is_hot"),
                    'home_type'=>I("post.home_type"),
                    'is_recommend'=>I("post.is_recommend"),
                    'desc'=>htmlspecialchars_decode(I("post.desc")),
                    'pictures'=>implode(',',I("post.pictures")),
                );
                if(empty($id)){
                    $result =$this->m_products->add($insert);
                }else{
                    $insert['update_time'] = date("Y-m-d H:i:s");
                    $result = $this->m_products->where(array("id"=>$id))->save($insert);
                }
                if ($result) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }




        // 文章删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_products->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

    }